package com.jinbiao.quasar.filter;

import cn.dev33.satoken.stp.StpUtil;
import com.jinbiao.quasar.util.AjaxJson;
import org.noear.solon.annotation.Component;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Filter;
import org.noear.solon.core.handle.FilterChain;
import org.noear.solon.core.handle.Result;

import java.util.HashMap;
import java.util.Map;

@Component(index = 0) //index 为顺序位（不加，则默认为0）
public class AppFilter implements Filter {
    @Override
    public void doFilter(Context ctx, FilterChain chain) throws Throwable {
        try {
            if (StpUtil.isLogin() || ctx.path().startsWith("/public/")) {
                //3.执行处理
                chain.doFilter(ctx);

                //3.2.未处理设为404状态
                if(!ctx.getHandled()){
                    ctx.render(AjaxJson.get(404, "资源不存在"));
                }
            } else {
                Map<String, Object> map = new HashMap<>();
                map.put("reload", true);
                ctx.render(AjaxJson.get(AjaxJson.CODE_NOT_LOGIN, "您已经退出，请重新登录", map));
            }

        } catch (Exception e){
            ctx.render(AjaxJson.get(AjaxJson.CODE_NOTFOUND_REQUEST,"你没有权限"));
        }
    }
}
